#include<bits/stdc++.h>
using namespace std;
const int N = 50001;

int n;
struct Node{
    int id,x;
    Node(){}
    Node(int id_,int x_):id{id_},x{x_}{}
    bool operator<(const Node& node)const{
        return x<node.x;
    }
};
Node nodes[N];
unordered_set<int> idm;
unordered_map<int,int> appear;
int sum = 0;

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&nodes[i].x,&nodes[i].id);
        if(!idm.count(nodes[i].id)){
            sum++;
            idm.insert(nodes[i].id);
        }
    }

    sort(nodes+1,nodes+n+1);
    // for(int i=1;i<=n;i++){
    //     cout<<"+++"<<nodes[i].x<<' '<<nodes[i].id<<"\n";
    // }
    int cur = 0;
    queue<int> que;
    int ans = INT_MAX;
    for(int i=1;i<=n;i++){
        int id = nodes[i].id;
        int x = nodes[i].x;
        que.push(i);
        if(!appear.count(id)) {
            cur++;
            appear[id] = 1;
        }
        else{
            appear[id]++;
        }
        while(!que.empty()&&appear[nodes[que.front()].id]>1){
            // cout<<"---"<<nodes[que.front()].x<<' '<<nodes[que.front()].id<<"\n";
            appear[nodes[que.front()].id]--;
            que.pop();
        }
        if(cur==sum){
            ans = min(ans,x-nodes[que.front()].x);
        }
    }
    printf("%d\n",ans);
    return 0;
}